
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T2:
*		The following code has been modified in order to remove surveyor fixed effects and survey month fixed effects from all regressions.
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t2
prog define maketable_t2
est clear
syntax varlist [if], [Table(string)] [SPEC11(varlist)] [SPEC21(varlist)]   [SPEC12(varlist)] [SPEC22(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [HET(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG][POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	
	*****************************
	*CONTROLS CLEAN UP
	*****************************
	*Another version of Winner that can be dropped for the control regressions since winner has no interpretation
		cap drop Winner_alt
		gen Winner_alt=Winner
	
	cap drop _*
	cap drop *AA* 
	
	local k=1
	if "`cont'"!="" {
	foreach var of local cont {
		sum `var'
		g AA`k'= `var'
		sum AA`k'
			local ++k
		}
	}

	foreach var of varlist AA*{
		sum `var'
		gen miss_`var'=`var'==.
		replace `var'=r(mean) if miss_`var'==1
		gen Winner_`var'=Winner*`var'
		gen mi_Winner_`var'=Winner*miss_`var'
		}

	local controls_final "Winner_AA* mi_Winner_AA*"
	
	*****************************
	*REGRESSION- LINEAR IN RANK SPEC 1
	*****************************
	
	local ester=1
	local eststr ""

	
	forvalues i=1/4 {
		if strpos("`spec1`i''", "") { 
			local nuspec1 "`i'"
			} 
		}
	
	foreach out in `varlist'  {		
	
		forvalues i=1/`nuspec1'{
			
			
			******************************	
			*LINEAR SPEC WITHOUT CONTROLS
			******************************
			
			xtreg `out'   `spec1`i'' Winner i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
			
			******************************	
			*TERCILE SPEC WITH CONTROLS
			******************************
		
			xtreg `out'   `spec1`i'' Winner_alt `controls_final' i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)


				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				
			}
		}
		
		local part2=`ester'+1
		
	esttab `eststr' using "`table'",  nostar     ///
		keep( `spec11' Winner) noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'")  ///
		postfoot( )


	*****************************
	*REGRESSION- TERCILES RANK SPEC 2
	*****************************
	
	local ester=1
	local eststr ""
		
	forvalues i=1/4 {
		if strpos("`spec2`i''", "") { 
			local nuspec2 "`i'"
			} 
		}
		
	foreach out in `varlist'  {		
		
		forvalues i=1/`nuspec2'{

		
			******************************	
			*TERCILE SPEC WITHOUT CONTROLS
			******************************
			
			xtreg `out' `spec2`i'' Winner i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				estadd local Lasso " "

				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
					estadd scalar No_Obs=e(N_g)
					local no_obs=e(N_g)

				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
					
					est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  `het'_Tercile_3= `het'_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
					
			******************************	
			*TERCILE SPEC WITH CONTROLS
			******************************
			
			xtreg `out'   `spec2`i'' Winner_alt `controls_final' i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)


				estadd local Lasso "X"
				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 

					estadd scalar No_Obs=`no_obs'
				
				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
					
					est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  `het'_Tercile_3= `het'_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
				
			}
			}
		local part2=`ester'+1
		
	esttab `eststr' using "`table'", nostar nonumbers nomtitles nostar order( `het'_Tercile_3 `het'_Tercile_2 `spec`i'' Winner) ///
		keep( `spec21' Winner) label noabbrev `type' append nogaps ///
		stats( `stats11' space   Cont_Mean Cont_Sd Lasso N2 No_Obs, fmt( `stats21' 2 2  2 0 0 0) layout( `stats31' @   "[@]"    @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " " "\hspace{-.1 in} Controls" " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
	drop Winner_alt *AA*

	end
	

*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T3
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
		
cap prog drop maketable_t3
prog define maketable_t3
est clear
syntax varlist [if], [Table(string)]  [SPEC11(varlist)] [SPEC21(varlist)]   [SPEC12(varlist)] [SPEC22(varlist)]    [CONT(varlist)] [MAIN(varlist)]  [HET(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG] [POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	
	local cmd "xtreg"
	if "`reg'"!="" local cmd "reg"

	local fe "fe i(Id)"
	if "`reg'"!="" local fe ""	

	local weight " [weight=Propensity_Score]"
	if "`reg'"!="" local weight ""
	
	local strata ""
	if "`reg'"!="" local strata "i.Final_Randomization_Cluster"	
	
	
	 
	local ester=1
	local eststr ""

	*****************************
	*REGRESSION- LINEAR IN RANK SPEC 1
	*****************************
	
	local ester=1
	local eststr ""

	
	forvalues i=1/4 {
		if strpos("`spec1`i''", "") { 
			local nuspec1 "`i'"
			} 
		}
	
	foreach out in `varlist'  {		
	
		forvalues i=1/`nuspec1'{
			
			
			******************************	
			*LINEAR SPEC WITHOUT CONTROLS
			******************************	
		
			`cmd' `out'   `spec1`i'' i.Surveyor_Code i.Survey_Version i.survey_month `strata' `if' `weight', clu(GroupNumber) `fe'

			
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				

						
				}
			}
			
		local part2=`ester'+1
		
	esttab `eststr' using "`table'",  nostar     ///
		keep( `spec11' ) noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'")  ///
		postfoot( )

		
	local ester=1
	local eststr ""
		
	*****************************
	*REGRESSION- TERCILES RANK SPEC 2
	*****************************
	
	local ester=1
	local eststr ""
		
	forvalues i=1/4 {
		if strpos("`spec2`i''", "") { 
			local nuspec2 "`i'"
			} 
		}
		
	foreach out in `varlist'  {		
		
		forvalues i=1/`nuspec2'{

		
			******************************	
			*TERCILE SPEC WITHOUT CONTROLS
			******************************
		
			`cmd' `out' `spec2`i'' i.Surveyor_Code i.Survey_Version i.survey_month `strata'  `if'  `weight', clu(GroupNumber) `fe'

				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
				if "`reg'"=="" {	
					estadd scalar No_Obs=e(N_g)
					}
					else {
						estadd scalar No_Obs=e(N)
						}
				
				estadd local space=""
				
				if "`reg'"=="" {
						qui sum `out' if Winner==0
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
						}
					else {
						qui sum `out' if Quint_Rank_NS_Tercile_1==0
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
						}
						
				est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  `het'_Tercile_3= `het'_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "

					
				}
			}
		local part2=`ester'+1
		
	esttab `eststr' using "`table'", nonumbers nomtitles nostar order( `het'_Tercile_3 `het'_Tercile_2 `spec`i'' Winner) ///
		keep( `spec21' ) label noabbrev `type' append nogaps ///
		stats( `stats11' space  Cont_Mean Cont_Sd  N2 No_Obs, fmt( `stats21' 2  2 0 0 0) layout( `stats31' @  "[@]"   @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " "  " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
		end
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T4
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t4
prog define maketable_t4
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]  [REG] [POSTHEAD(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	local ester=1
	local eststr ""

	local cmd "xtreg"
	if "`reg'"!="" local cmd "reg"

	local fe "fe i(Id)"
	if "`reg'"!="" local fe ""

	forvalues i=1/4 {
	if strpos("`spec`i''", "") { 
	local nuspec "`i'"
		display "`i'"
		} 
		}
		
	foreach out in `varlist'  {		
	
		forvalues i=1/`nuspec'{
			
			local test1: word 1 of  `spec`i''
		
			local test2: word 2 of  `spec`i''
			
			xi: `cmd' `out' `main' `cont' `spec`i'' Winner i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) `fe'

			
					test `test1'=`test2'
			
							local n1= r(p)
							local star ""
								if `n1'<=.01 local star "\sym{***}"
								if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
								if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
							local n2 = trim("`: display %10.3f `n1''")
							local combo "`n2'"
								estadd local p4 `combo'	
								
					estadd scalar N2=e(N)
						distinct Id `if' & `out'!=. 
							estadd scalar No_Obs=e(N_g)
							
							estadd local space=""
							qui sum `out' if Winner==0
								estadd scalar Cont_Mean=r(mean)
								estadd scalar Cont_Sd=r(sd)
								
								est store E`ester'
							
							local eststr "`eststr' E`ester'"
							local ++ester
		

								
						local stats11 "space   p4 space "
						local stats21 " 0  2 2 2 "
						local stats31 " @  @ @ @"
						local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile=" "\hspace{0.1in}Winner*Middle Tercile"   "			
			}
			}
		local part2=`ester'+1
esttab `eststr' using "`table'",    nostar   ///
		keep(`main' `spec1' `spec2' `spec3' `spec4' Winner) label noabbrev `type' replace nogaps ///
		drop(`cont') ///
	stats( `stats11' space  Cont_Mean Cont_Sd  N2 No_Obs, fmt( `stats21' 2  2 0 0 0) layout( `stats31' @  "[@]"   @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " "  " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead("`posthead'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'") /// 
		postfoot( `postfoot1' \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end


*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T4
*		The following code has been modified in order to remove surveyor fixed effects and survey month fixed effects from all regressions.
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
		
		cap prog drop maketable_t4_2
prog define maketable_t4_2
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]  [REG] [POSTHEAD(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	local ester=1
	local eststr ""

	local cmd "xtreg"
	if "`reg'"!="" local cmd "reg"

	local fe "fe i(Id)"
	if "`reg'"!="" local fe ""
	
			

	
	
		forvalues j=1/4{
		    
			local out: word `j' of `varlist'
			
			gen Winner_MR_Prediction_Obs_T3=Winner_MR_Prediction`j'_Obs_T3
			gen Winner_MR_Predict_Obs_Rank_T3=Winner_MR_Predict`j'_Obs_Rank_T3
		    gen Winner_MR_Prediction_Obs_T2=Winner_MR_Prediction`j'_Obs_T2
			gen Winner_MR_Predict_Obs_Rank_T2=Winner_MR_Predict`j'_Obs_Rank_T2
			

			
			xi: `cmd' `out' `main' `cont' Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Winner i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) `fe'

			
					test Winner_MR_Prediction_Obs_T3=Winner_MR_Prediction_Obs_T2
			
							local n1= r(p)
							local star ""
								if `n1'<=.01 local star "\sym{***}"
								if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
								if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
							local n2 = trim("`: display %10.3f `n1''")
							local combo "`n2'"
								estadd local p4 `combo'	
								
					estadd scalar N2=e(N)
						distinct Id `if' & `out'!=. 
							estadd scalar No_Obs=e(N_g)
							
							estadd local space=""
							qui sum `out' if Winner==0
								estadd scalar Cont_Mean=r(mean)
								estadd scalar Cont_Sd=r(sd)
								
								est store E`ester'
							
							local eststr "`eststr' E`ester'"
							local ++ester
		

								
						local stats11 "space   p4 space "
						local stats21 " 0  2 2 2 "
						local stats31 " @  @ @ @"
						local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile=" "\hspace{0.1in}Winner*Middle Tercile"   "	
	
	
			xi: `cmd' `out' `main' `cont' Winner_MR_Predict_Obs_Rank_T3 Winner_MR_Predict_Obs_Rank_T2 Winner i.Survey_Version `if'  [weight=Propensity_Score], clu(GroupNumber) `fe'

			
					test Winner_MR_Predict_Obs_Rank_T3=Winner_MR_Predict_Obs_Rank_T2
			
							local n1= r(p)
							local star ""
								if `n1'<=.01 local star "\sym{***}"
								if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
								if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
							local n2 = trim("`: display %10.3f `n1''")
							local combo "`n2'"
								estadd local p4 `combo'	
								
					estadd scalar N2=e(N)
						distinct Id `if' & `out'!=. 
							estadd scalar No_Obs=e(N_g)
							
							estadd local space=""
							qui sum `out' if Winner==0
								estadd scalar Cont_Mean=r(mean)
								estadd scalar Cont_Sd=r(sd)
								
								est store E`ester'
							
							local eststr "`eststr' E`ester'"
							local ++ester
		

								
						local stats11 "space   p4 space "
						local stats21 " 0  2 2 2 "
						local stats31 " @  @ @ @"
						local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile=" "\hspace{0.1in}Winner*Middle Tercile"   "	
						
			drop Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2
			
		}
		local part2=`ester'+1
esttab `eststr' using "`table'",   nostar    ///
		keep ( Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2 Winner)  ///
		order( Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2 Winner) label noabbrev `type' replace nogaps ///
		drop(`cont') ///
		coeflabels ( Winner_MR_Prediction_Obs_T3 "\specialcell{\enspace Winner*Top Tercile Controls}" ///
		Winner_MR_Prediction_Obs_T2 "\specialcell{\enspace Winner*Top Middle Controls}" ///
		Winner_MR_Predict_Obs_Rank_T3 "\specialcell{\enspace Winner*Top Tercile Controls+Rank}"  ///
		Winner_MR_Predict_Obs_Rank_T2 "\specialcell{\enspace Winner*Top Middle Controls+Rank}") ///
	stats( `stats11' space  Cont_Mean Cont_Sd  N2 No_Obs, fmt( `stats21' 2  2 0 0 0) layout( `stats31' @  "[@]"   @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " "  " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead("`posthead'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'") /// 
		postfoot( `postfoot1' \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end
		
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A1 (BALANCE CHECK)
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
cap prog drop baltest
prog define baltest 
est clear
syntax [if], PANELA(varlist) [PANELB(varlist)] [PANELC(varlist)] [PANELD(varlist)] [Table(string)] [CONT(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [GPLEVELPANEL(string)] [SPECIALCONTVARS(varlist)] [SPECIALCONT(varlist)] [REFERENCE(string)] [TREATMENT(varlist)]

	local treatments `treatment'
		local num : word count `treatment'
	
	forvalues j=1/`num' {
	local treatlist: word `j' of `treatments'
	display "`treatlist'"
	
	global suestestimates 
	global suesttest 	
	global ftest
	
foreach cov in DVmean`j' `treatlist'  N {

			local i=1

foreach var in `panela' `panelb' `panelc' `paneld' {
   

		areg `var'  `treatlist'  `if' `weight'  , clu(GroupNumber) a(Final_Randomization_Cluster) 

		
		if  "`cov'"!="N" & "`cov'"!="DVmean`j'" {
			local b= _b[`cov']
			local n1= r(p)
			local se = _se[`cov']
			
// 			local star ""
// 				if `n1'<=.01 local star "\sym{***}"
// 				if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
// 				if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
			local n2 = trim("`: display %10.3f `b''") 
			local `var'_combo "`n2'"
			
			local se = trim("`: display %10.3f `se''") 
			local `var'_se "(`se')"
			
			reg `treatlist' `var' i.Final_Randomization_Cluster  `if'
					
			estimates store m`i'_`j'
			
			global suestestimates $suestestimates m`i'_`j'
			
			display "$suestestimates"
			
			if `i'<=$number -1 {
				local suesttestlocal "[m`i'_`j'_mean]`var'="
				display "`suesttestlocal'"
				}	
			else {
				local suesttestlocal "[m`i'_`j'_mean]`var'"
				display "`suesttestlocal'"
			}
			global suesttest $suesttest `suesttestlocal'
			display "$suesttest"
			local i=`i'+1
					
			}

		if "`cov'"=="N" {
			local b= e(N)
			local n1=500
			local n2 = trim("`: display %10.0f `b''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
		sum `var' if `treatlist'==0
		if "`cov'"=="DVmean`j'" {
			local mn=r(mean)
			local n2 = trim("`: display %10.3f `mn''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
	
	

}
foreach var in `panela' `panelb' `panelc' `paneld' {
	estadd local b_`var' ``var'_combo' 
	estadd local se_`var' ``var'_se'
}

estadd local PA ""
estadd local PB ""
estadd local PC ""
estadd local PD ""

est store `cov'
}


	suest $suestestimates, cluster( GroupNumber)
	test $suesttest
	global rp=trim("`: display %10.3f `r(p)''")
	display "$rp"
	estadd local ftest "$rp"
	local Ftest "`ftest'"

local cellsA "" 
foreach var in `panela' {
    local cellsA "`cellsA' b_`var' se_`var'"
}
local cellsB "" 
foreach var in `panelb' {
    local cellsB "`cellsB' b_`var' se_`var'"
}
local cellsC "" 
foreach var in `panelc' {
    local cellsC "`cellsC' b_`var' se_`var'"
}
local cellsD "" 
foreach var in `paneld' {
    local cellsD "`cellsD' b_`var' se_`var'"
}

	}
	
local labstr "\textit{Panel A: Individual Characteristics of Ranked Entrepreneur}"
	foreach out in `panela' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
if "`panelb'"!="" {
	local labstr `"`labstr'" "\textit{Panel B: Sector of Ranked Entrepreneur}"'
	foreach out in `panelb' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`panelc'"!="" {
	local labstr `"`labstr'" "\textit{Panel C: Household Characteristics}"'
	foreach out in `panelc' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`paneld'"!="" {
	local labstr `"`labstr'" "\textit{Panel D: Characteristics of Household Businesses}"'
	foreach out in `paneld' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}


di "`labstr'"
di "`panelc'"
	
	esttab DVmean1  Revealed DVmean2 Incentives DVmean3  Public DVmean4 Winner2 N using "`table'", noabbrev `type' replace nogaps /// 
		mlab("\specialcell{No Stakes \\ Mean}"  "\specialcell{Stakes \\ Difference}" ///
		"\specialcell{No Incentives \\ Mean}"  "\specialcell{Incentive \\ Difference}" ///
		"\specialcell{Private \\ Mean}"  "\specialcell{Public \\ Difference}" ///
		"\specialcell{Grant Loser \\ Mean}"  "\specialcell{Grant Winner \\ Difference}" ///
		"N") /// 
	stats(PA `cellsA' PB `cellsB' PC `cellsC' PD `cellsD', labels("`labstr'")) keep( ) ///
		nonote   noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'10`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		end
	
		
		
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A3 (MAIN EFFECT)
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t_maineffect
prog define maketable_t_maineffect
est clear
syntax varlist [if], [Table(string)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	
	local ester=1
	local eststr ""
	
	foreach out in `varlist'  {		
	
		xi: xtreg `out' Winner `cont'  i.Survey_Version i.Surveyor_Code   i.survey_month `if' [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

		estadd scalar N2=e(N)

		estadd local space=" "
		qui sum `out' 
			estadd scalar Cont_Mean=r(mean)
			estadd scalar Cont_Sd=r(sd)
		
		distinct Id if `out'!=. & Done==1
			estadd scalar No_Obs=r(ndistinct)		
			
				
			est store E`ester'
		
		local eststr "`eststr' E`ester'"
		local ++ester
		}
	
	local part2=`ester'+1
	
	esttab `eststr' using "`table'", nostar keep( Winner ) label noabbrev `type' replace nogaps ///
		stats( space Cont_Mean Cont_Sd space N2 No_Obs, fmt( 2 2 2 0 0) layout( @ @ "[@]" @ @ @) /// 
		labels(" " "Mean of Outcome" "for Grant Losers" " " "N" "No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{center, max width=1\linewidth, max totalheight=1\textheight} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end
		

*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A5 (MAIN EFFECT ANCOVA)
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t_maineffectancova
prog define maketable_t_maineffectancova
est clear
syntax varlist [if], [Table(string)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	
	local ester=1
	local eststr ""

		foreach out in `varlist'  {		

			reg `out' Winner `cont' B_`out' i.Final_Randomization_Cluster i.Surveyor_Code i.Survey_Version  i.survey_month `if' [weight=Propensity_Score], clu(GroupNumber) 
		

			estadd scalar N2=e(N)

			estadd local space=" "
			qui sum `out' 
				estadd scalar Cont_Mean=r(mean)
				estadd scalar Cont_Sd=r(sd)
			
			distinct Id if `out'!=. & Done==1
				estadd scalar No_Obs=r(ndistinct)		
				
					
				est store E`ester'
		
			local eststr "`eststr' E`ester'"
			local ++ester
		}
		
		local part2=`ester'+1
esttab `eststr' using "`table'", nostar keep( Winner ) label noabbrev `type' replace nogaps ///
		stats( space Cont_Mean Cont_Sd space N2 No_Obs, fmt( 2 2 2 0 0) layout( @ @ "[@]" @ @ @) /// 
		labels(" " "Mean of" "Outcome" " " "N" "No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{center, max width=1\linewidth, max totalheight=1\textheight} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end
		
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A6 (MAIN EFFECT ANCOVA)
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t0_ancova
prog define maketable_t0_ancova
est clear
syntax varlist [if], [Table(string)] [SPEC11(varlist)] [SPEC21(varlist)] [SPEC3(varlist)] [SPEC4(varlist)] [SPEC1PB(varlist)] [SPEC2PB(varlist)] [SPEC3PB(varlist)] [SPEC4PB(varlist)]   [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG][POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	
	cap drop _*
	cap drop *AA* 
	
	local k=1
	if "`cont'"!="" {
	foreach var of global controls {
		sum `var'
		g AA`k'= `var'
		sum AA`k'
			local ++k
		}
	}

	foreach var of varlist AA*{
		sum `var'
		gen miss_`var'=`var'==.
		replace `var'=r(mean) if miss_`var'==1
		}

	
	
	local ester=1
	local eststr ""

	
	
	
	foreach out in `varlist'  {		
	forvalues i=1/`nuspec'{

		
	reg `out' `main' `spec1`i'' i.Final_Randomization_Cluster i.Surveyor_Code i.Survey_Version i.survey_month  B_`out' `if'  [weight=Propensity_Score], clu(GroupNumber)

				
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				
		

			reg `out' `main' `spec1`i'' i.Final_Randomization_Cluster i.Surveyor_Code i.Survey_Version i.survey_month AA* miss_AA* B_`out' `if'  [weight=Propensity_Score], clu(GroupNumber)


				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				
			}
			}
		local part2=`ester'+1
esttab `eststr' using "`table'",       ///
		 keep( `spec11' Winner) nostar noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'")  ///
		postfoot( )

		
	local ester=1
	local eststr ""
		
	foreach out in `varlist'  {		
	forvalues i=1/`nuspec'{

		
			reg `out' `main' `spec2`i'' i.Final_Randomization_Cluster i.Surveyor_Code i.Survey_Version i.survey_month  B_`out' `if'  [weight=Propensity_Score], clu(GroupNumber)

				estadd local Lasso " "

				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
					estadd scalar No_Obs=r(ndistinct)-1
				
				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
					
					est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  `ologit'_Tercile_3= `ologit'_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
					
					local Lasso_Final "Lasso"
		
			reg `out' `main' `spec2`i'' i.Final_Randomization_Cluster i.Surveyor_Code i.Survey_Version i.survey_month AA* miss_AA* B_`out' `if'  [weight=Propensity_Score], clu(GroupNumber)


				estadd local Lasso "X"
				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
					estadd scalar No_Obs=r(ndistinct)-1
				
				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
					
					est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  `ologit'_Tercile_3= `ologit'_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
				

					
			}
			}
		local part2=`ester'+1
esttab `eststr' using "`table'", nonumbers nomtitles nostar order( `ologit'_Tercile_3 `ologit'_Tercile_2 `spec`i'' Winner) ///
		keep( `spec21' Winner) label noabbrev `type' append nogaps ///
		stats( `stats11' space   Cont_Mean Cont_Sd Lasso N2 No_Obs, fmt( `stats21' 2 2  2 0 0 0) layout( `stats31' @   "[@]"    @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " " "\hspace{-.1 in} Controls" " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
	end

						
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A7 NUMBER OF RANKS
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************


cap prog drop maketable_ta7
prog define maketable_ta7
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)] [SPEC5(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]  [REG] [POSTHEAD(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	local ester=1
	local eststr ""

	local cmd "xtreg"
	if "`reg'"!="" local cmd "reg"

	local fe "fe i(Id)"
	if "`reg'"!="" local fe ""
		
	forvalues j=1/4 {
	if strpos("`spec`j''", "") { 
	local nuspec "`j'"
		display "`j'"
		} 
		}
		
	foreach out in `varlist'  {		
	local k=1
	forvalues i=1/`nuspec'{

	xtreg `out'  `spec`i'' Winner i.Surveyor_Code i.Survey_Version i.survey_month if Group_Size==5& Survey_Version!=5 &  Done==1 &Quint_Rank_NS!=.   [weight=Propensity_Score], clu(GroupNumber) fe
				

						if `i'==2{
							estadd local p4 "$num2"
						}		
						if `i'==3{
							estadd local p4 "$num3"
						}	
						if `i'==4{
							estadd local p4 "$num4"
						}	
				
							
				estadd scalar N2=e(N)
					distinct Id `if' & `out'!=. 
						estadd scalar No_Obs=e(N_g)
						
						estadd local space=""
						qui sum `out' if Winner==0
							estadd scalar Cont_Mean=r(mean)
							estadd scalar Cont_Sd=r(sd)
							
							est store E`ester'
						
						local eststr "`eststr' E`ester'"
						local ++ester
	

							
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from test }"  "\hspace{0.1in}Winner*Rank (Avg 4 Ranks)=" "\hspace{0.1in}Winner*Rank (Avg X Ranks)"   "			
			}
			local k=`k'+1
			}
		local part2=`ester'+1
esttab `eststr' using "`table'",  nostar     ///
		keep(`spec1' `spec2' `spec3' `spec4'  `spec5' Winner) order(`spec1' `spec2' `spec3' `spec4'  `spec5' Winner) label noabbrev `type' replace nogaps ///
		drop(`cont') ///
	stats(   `stats11'   space  Cont_Mean Cont_Sd  N2 No_Obs, fmt( `stats21' 2  2 0 0 0) layout( `stats31' @  "[@]"   @ @) /// 
		labels(  `stats41'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " "  " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead("`posthead'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'") /// 
		postfoot( `postfoot1' \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end


*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A11- sd and rank
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

		
cap prog drop maketable_ta11
prog define maketable_ta11
est clear
syntax varlist [if], [Table(string)] [SPEC11(varlist)] [SPEC21(varlist)]   [SPEC12(varlist)] [SPEC22(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [HET(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG][POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	

	*****************************
	*REGRESSION- LINEAR IN RANK SPEC 1
	*****************************
	
	local ester=1
	local eststr ""

	
	forvalues i=1/4 {
		if strpos("`spec1`i''", "") { 
			local nuspec1 "`i'"
			} 
		}
	
	foreach out in `varlist'  {		
	
		forvalues i=1/`nuspec1'{
			
			
			******************************	
			*LINEAR SPEC WITHOUT CONTROLS
			******************************
			
			xtreg `out'   `spec1`i''  i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				
			}
		}
		
		local part2=`ester'+1
		
	esttab `eststr' using "`table'",  nostar     ///
		keep( `spec11' Winner) noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'")  ///
		postfoot( )


	*****************************
	*REGRESSION- TERCILES RANK SPEC 2
	*****************************
	
	local ester=1
	local eststr ""
		
	forvalues i=1/4 {
		if strpos("`spec2`i''", "") { 
			local nuspec2 "`i'"
			} 
		}
		
	foreach out in `varlist'  {		
		
		forvalues i=1/`nuspec2'{

		
			******************************	
			*TERCILE SPEC WITHOUT CONTROLS
			******************************
			
			xtreg `out' `spec2`i''  i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				estadd local Lasso " "

				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
					estadd scalar No_Obs=e(N_g)
					local no_obs=e(N_g)

				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
					
					est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
			
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
						
					
					local stats11 "space   p4 space "
					local stats21 " 0  2 2 2 "
					local stats31 " @  @ @ @"
					local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
					
	
				
			}
			}
		local part2=`ester'+1
		
	esttab `eststr' using "`table'", nonumbers nomtitles nostar order( `spec21' Winner) ///
		keep( `spec21' Winner) label noabbrev `type' append nogaps ///
		stats(space   Cont_Mean Cont_Sd  N2 No_Obs, fmt( 2 2   2 0 0) layout(  @ @  "[@]"    @ @) /// 
		labels(  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " " " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
	

	end
	


		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: 1 
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
			
			
	
cap prog drop maketable_t1
prog define maketable_t1
est clear
syntax varlist [if], [Table(string)]  [SPEC(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [HET(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG] [POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]


	
	 
	local ester=1
	local eststr ""


	*****************************
	*PANEL A -  LEVEL ON LEVEL
	*****************************
	
	foreach out in `varlist'  {			
			

		
				xi: areg `out'   i.Surveyor_Code   i.survey_month i.q  `cont' `spec' `if' , clu(GroupNumber)a(Final_Randomization_Cluster)

			
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				

						
				
			}
			
		local part2=`ester'+1
		
	esttab `eststr' using "`table'",  nodep nostar     ///
		keep( `spec' ) noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2' ")  ///
		postfoot( )

		

	*****************************
	*PANEL B - PERCENTILE ON PERCENTILE
	*****************************
	
	local ester=1
	local eststr ""

		
	foreach out in `varlist'  {		
		
		xi: areg `out'_Pct   i.Surveyor_Code   i.survey_month i.q  `cont' `spec'_Pct `if' , clu(GroupNumber)a(Final_Randomization_Cluster)
		


		estadd scalar N2=e(N)
		
		sum `out' 
		estadd scalar Control_Mean=r(mean)
		estadd scalar Control_SD=r(sd)

		
		distinct Id if `out'!=.
		estadd scalar N3=r(ndistinct)
		
		estadd local space=""

				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester

					
				
			}
		local part2=`ester'+1
		

		
		
	esttab `eststr' using "`table'", nodep nonumbers nomtitles nostar  ///
		keep( 	`spec'_Pct ) label noabbrev `type' append nogaps ///
		stats( space  Control_Mean Control_SD  N2 N3, fmt( 2  2 0 0 0) layout(@  @  "[@]"   @ @) /// 
		labels( " "  "\hline \hspace{-.1 in} Mean of Outcome" " "  " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
		end
	
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: 5
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
		
cap prog drop maketable_t5
prog define maketable_t5
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)]
	


	local ester=1
	local eststr ""
	
	forvalues i=1/4 {
	if strpos("`spec`i''", "") { 
	local nuspec "`i'"
		display "`i'"
		} 
		}
	
	forvalues i=1/`nuspec'{	
	foreach out in `varlist'  {		
	
	
					areg `out' `spec`i'' `main' i.Surveyor_Code i.Survey_Version i.survey_month i.q `if'  , clu(GroupNumber)a(Final_Randomization_Cluster)
				
						estadd scalar N2=e(N)
						distinct Id if `out'!=.  
						estadd scalar No_Obs=r(ndistinct)
						
						estadd local space=""
						qui sum `out' 
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
					
						
					
						est store E`ester'
						local eststr "`eststr' E`ester'"
						local ++ester
						
									
						local nv : word count `cont'
						
					
						
						
						

		}
		}

		
esttab `eststr' using "`table'", nostar drop(*q* *Survey* *survey* _cons `cont' `main' ) label noabbrev `type' replace nogaps ///
		stats(  space  N2 No_Obs, fmt(  0 0 0) layout( @ @ @ ) /// 
		labels(  " " "N" "No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end

*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: 6
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************		
	
cap prog drop maketable_t6a
prog define maketable_t6a
est clear
syntax varlist [if], [Table(string)] [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NOINTERACTION]
	
	preserve
	replace Avg_AllRank_NS_Pct=AllRank_Pct if Self==1
	replace Avg_RankNS_Pct_Public=Rank_Pct_Public if Self==1
	replace Avg_RankNS_Pct_Incentives=Rank_Pct_Incentives if Self==1
	replace Avg_RankNS_Pct_Incentives_Public=Rank_Pct_Public_Incentives if Self==1

	local ester=1
	local eststr ""
	if "`nointeraction'"==""{
		local spec1 "   Avg_AllRank_NS_Pct Avg_RankNS_Pct_Public Avg_RankNS_Pct_Incentives Avg_RankNS_Pct_Incentives_Public Public_Incentives    Public   Incentives "
		local spec2 "   Avg_AllRank_NS_Pct Avg_RankNS_Pct_Public Avg_RankNS_Pct_Incentives Avg_RankNS_Pct_Incentives_Public Public_Incentives    Public   Incentives "
	}
		else {
			local spec1 "   Avg_AllRank_NS_Pct Avg_RankNS_Pct_Public      Public    "
			local spec2 "   Avg_AllRank_NS_Pct Avg_RankNS_Pct_Incentives       Incentives "
		}
	foreach out in `varlist'  {		
	
		forvalues k=1/2{
		

	

			if `k'==1 {
				
			
				local conditions ""==0" "==1"
				
				local names " "No Stakes"  "Stakes""
					forvalues j=1/2{
					
						local lab: word `j' of `names'
						

				
					local cond: word `j' of `conditions'
					
						
						areg `out' `spec1' i.Surveyor_Code i.Survey_Version i.survey_month i.q if Self==1 & Revealed`cond' & q<=8 & q>=3  , clu(GroupNumber)a(Final_Randomization_Cluster)
				
						*lincom Avg_AllRank_NS_Pct -( Avg_RankNS_Pct_Public+Avg_RankNS_Pct_Incentives  +Avg_RankNS_Pct_Incentives_Public)

										
						estadd scalar N2=e(N)
						distinct Id if `out'!=.  &  Self==1
						estadd scalar No_Obs=r(ndistinct)
						
						estadd local space=""
						qui sum `out' 
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
						
						estadd local sample "Not Self"
						estadd local Treatment "`lab'"

						
						est store E`ester'
						local eststr "`eststr' E`ester'"
						local ++ester
						
									
						local nv : word count `cont'
						}
						
						}

			if `k'==2 {
				
				
				local conditions " "==0" "==1""
				
				local names ""No Stakes"  "Stakes""
					forvalues j=1/2{
					
						local lab: word `j' of `names'
						
								local cond: word `j' of `conditions'
		
						
						areg `out' `spec2' i.Surveyor_Code i.Survey_Version i.survey_month i.q if Self==0 & Revealed`cond' & q<=8 & q>=3, clu(GroupNumber)a(Final_Randomization_Cluster)
									
						*lincom Avg_AllRank_NS_Pct -( Avg_RankNS_Pct_Public+Avg_RankNS_Pct_Incentives  +Avg_RankNS_Pct_Incentives_Public)

						estadd scalar N2=e(N)
						distinct Id if `out'!=.  & Self==0
						estadd scalar No_Obs=r(ndistinct)
						
						estadd local space=""
						qui sum `out' 
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
					
						estadd local sample "Not Self"
						estadd local Treatment "`lab'"
					
						est store E`ester'
						local eststr "`eststr' E`ester'"
						local ++ester
						
									
						local nv : word count `cont'
						
						}
						}
						

		}
		}

		restore
esttab `eststr' using "`table'", nostar order(`main' ) drop(*q* *Survey* *survey* _cons `cont'  Public* Incentives) label noabbrev `type' replace nogaps ///
		stats( `stats12' `stats11' space sample Treatment space  N2 No_Obs, fmt(`stats22' `stats21' 2 2 2 2  0 0 0) layout(`stats32' `stats31' @ @ "[@]" @ @) /// 
		labels( `stats42'  `stats41'  " "  "Who is Ranked?" "Treatment"  " "    "N" "No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A3
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************		
	
cap prog drop maketable_tmatches
prog define maketable_tmatches
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] [PREHEAD2(string)] [TITLE(string)] [LABel(string)] [HET(varlist)] 

	local ester=1
	local eststr ""
	
	foreach out in `varlist'  {		
	
		local j=1
		
		foreach var in `het'  {		
	
			gen Matches= R_`var'
			gen MatchesAllRankNSPct=Matches*AllRank_NS_Pct
			
			
	
					areg `out'  MatchesAllRankNSPct AllRank_NS_Pct Matches   i.Surveyor_Code  i.survey_month i.q if `var'==1 & (q==7|q==8|q==12|q==13 )  , clu(GroupNumber)a(Final_Randomization_Cluster)
				
						estadd scalar N2=e(N)
						distinct Id if `out'!=.  & Done==1 & `var'==1 & (q==7|q==8|q==12|q==13)
						estadd scalar No_Obs=r(ndistinct)
						
						estadd local space=""
						qui sum `out' 
						estadd scalar Cont_Mean=r(mean)
						estadd scalar Cont_Sd=r(sd)
						
						local lab: variable label `var' 
						estadd local Characteristic "\specialcell{`lab'}"
					
						est store E`ester'
						local eststr "`eststr' E`ester'"
						local ester2 =`ester'+1
						local ++ester
						
					
					
					drop Matches MatchesAllRankNSPct				
						local j=`j'+1
		}
			
		}
	
esttab `eststr' using "`table'", nostar order(`main' ) ///
		coeflab (MatchesAllRankNSPct "Characteristic*Rank") ///
		drop(*q* *Survey* *survey* _cons `cont' Matches ) ///
		 nomtitles nostar   ///
		label noabbrev `type' replace nogaps ///
		stats(Characteristic  space  N2 No_Obs, fmt(  0 0 0) layout( @ @ @ ) /// 
		labels( "\\ Characteristic" " " "\hline N" "No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable}  \caption{`title'} \label{`label'} { \begin{tabular}{l*{6}{c}} \hline\hline & \multicolumn{5}{c}{Pooled Questions} \\ \cmidrule(lr){2-6} 					") /// 
		postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end
		
		
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: A21
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************		
	
cap prog drop maketable_hetero_incpub
prog define maketable_hetero_incpub
est clear
syntax varlist [if], [Table(string)] [MAIN(varlist)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	

	local ester=1
	local eststr ""
	tokenize `ologit'
	
	local spec1 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=."
	local spec2 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==1"
	local spec3 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==0"

	
	local names ""Pooled""Stakes"  "No Stakes""
					
	foreach out in `varlist'  {		
	
		foreach c in `main'  {	
			forvalues i=1/3{
			cap drop Characteristic  CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
			gen Characteristic=`c'
			gen CharacteristicPublicIncentives=Characteristic*Public*Incentives
			gen CharacteristicPublic=Characteristic*Public
			gen CharacteristicIncentives=Characteristic*Incentives
		
			local lab: word `i' of `names'


			xi: areg `out' `spec`i'' & q>=3 & q<=8 , clu(GroupNumber)a(Final_Randomization_Cluster)
		
		estadd scalar N2=e(N)

		estadd local Treatment "`lab'"
		local lab:  variable label `c' 
		estadd local sample="`lab'"
		
		sum `out' 
		estadd scalar Control_Mean=r(mean)
		estadd scalar Control_SD=r(sd)

		
		distinct Id if `out'!=.
		estadd scalar N3=r(ndistinct)
		
		estadd local space=""


		est store E`ester'
		
		local eststr "`eststr' E`ester'"
		local ++ester
		}
		drop CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
		
		}
		}
		local part2=`ester'+1
esttab `eststr' using "`table'", nostar keep(CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives ///
  Characteristic  ) label noabbrev `type' replace nogaps ///
		coeflabels(CharacteristicPublicIncentives "Characteristic*Public*Incentives"  CharacteristicIncentives "Characteristic*Incentives" ///
		CharacteristicPublic "Characteristic*Public" Characteristic   "Characteristic" ///
		Public "Public" Incentives "Incentives") ///
			stats( space Control_Mean Control_SD space sample Treatment space N2 N3, fmt( 2 2 2 2 0 0) layout( @ @ [@] @ @ @) /// 
		labels(" " "Mean of" "Outcome" " " "Characteristic" "Treatment" " " "N" "No. HHs" )) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1.3\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end					

		
		
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************	
cap prog drop baltest2
prog define baltest2
est clear
syntax [if], PANELA(varlist) [PANELB(varlist)] [PANELC(varlist)] [PANELD(varlist)] [Table(string)] [CONT(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [GPLEVELPANEL(string)] [SPECIALCONTVARS(varlist)] [SPECIALCONT(varlist)] [REFERENCE(string)] [TREATMENT(varlist)]

	local treatments `treatment'
		local num : word count `treatment'
	
	forvalues j=1/`num' {
	local treatlist: word `j' of `treatments'
	display "`treatlist'"
	
	global suestestimates 
	global suesttest 	
	global ftest
	
foreach cov in DVmean`j' `treatlist'  N {

			local i=1

foreach var in `panela' `panelb' `panelc' `paneld' {
   

		areg `var'  `treatlist'  `if' `weight'  , clu(GroupNumber) a(Final_Randomization_Cluster) 

		
		if  "`cov'"!="N" & "`cov'"!="DVmean`j'" {
			local b= _b[`cov']
			local n1= r(p)
			local se = _se[`cov']
			
// 			local star ""
// 				if `n1'<=.01 local star "\sym{***}"
// 				if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
// 				if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
			local n2 = trim("`: display %10.3f `b''") 
			local `var'_combo "`n2'"
			
			local se = trim("`: display %10.3f `se''") 
			local `var'_se "(`se')"
			
			reg `treatlist' `var' i.Final_Randomization_Cluster  `if'
					
			estimates store m`i'_`j'
			
			global suestestimates $suestestimates m`i'_`j'
			
			display "$suestestimates"
			
			if `i'<=$number -1 {
				local suesttestlocal "[m`i'_`j'_mean]`var'="
				display "`suesttestlocal'"
				}	
			else {
				local suesttestlocal "[m`i'_`j'_mean]`var'"
				display "`suesttestlocal'"
			}
			global suesttest $suesttest `suesttestlocal'
			display "$suesttest"
			local i=`i'+1
					
			}

		if "`cov'"=="N" {
			local b= e(N)
			local n1=500
			local n2 = trim("`: display %10.0f `b''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
		sum `var' if `treatlist'==0
		if "`cov'"=="DVmean`j'" {
			local mn=r(mean)
			local n2 = trim("`: display %10.3f `mn''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
	
	

}
foreach var in `panela' `panelb' `panelc' `paneld' {
	estadd local b_`var' ``var'_combo' 
	estadd local se_`var' ``var'_se'
}

estadd local PA ""
estadd local PB ""
estadd local PC ""
estadd local PD ""

est store `cov'
}


	suest $suestestimates, cluster( GroupNumber)
	test $suesttest
	global rp=trim("`: display %10.3f `r(p)''")
	display "$rp"
	estadd local ftest "$rp"
	local Ftest "`ftest'"

local cellsA "" 
foreach var in `panela' {
    local cellsA "`cellsA' b_`var' se_`var'"
}
local cellsB "" 
foreach var in `panelb' {
    local cellsB "`cellsB' b_`var' se_`var'"
}
local cellsC "" 
foreach var in `panelc' {
    local cellsC "`cellsC' b_`var' se_`var'"
}
local cellsD "" 
foreach var in `paneld' {
    local cellsD "`cellsD' b_`var' se_`var'"
}

	}
	
local labstr "\textit{Panel A: Individual Characteristics of Ranked Entrepreneur}"
	foreach out in `panela' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
if "`panelb'"!="" {
	local labstr `"`labstr'" "\textit{Panel B: Sector of Ranked Entrepreneur}"'
	foreach out in `panelb' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`panelc'"!="" {
	local labstr `"`labstr'" "\textit{Panel C: Household Characteristics}"'
	foreach out in `panelc' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`paneld'"!="" {
	local labstr `"`labstr'" "\textit{Panel D: Characteristics of Household Businesses}"'
	foreach out in `paneld' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}


di "`labstr'"
di "`panelc'"
	
	esttab DVmean1  Winner_Quint_T_3 DVmean2 Winner_Quint_T_2 DVmean3  Winner_Quint_T_1  using "`table'", noabbrev `type' replace nogaps /// 
		mlab("\specialcell{Bottom Tercile \\ No Grant\\ Mean}"  "\specialcell{Bottom Tercile \\ Grant \\ Difference}" ///
		"\specialcell{Middle Tercile \\ No Grant\\ Mean}"  "\specialcell{Middle Tercile \\ Grant \\ Difference}" ///
		"\specialcell{Top Tercile \\ No Grant\\ Mean}"  "\specialcell{Top Tercile \\ Grant \\ Difference}") /// 
	stats(PA `cellsA' PB `cellsB' PC `cellsC' PD `cellsD', labels("`labstr'")) keep( ) ///
		nonote   noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'10`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		end
		

		
			
cap prog drop baltest3
prog define baltest3
est clear
syntax [if], PANELA(varlist) [PANELB(varlist)] [PANELC(varlist)] [PANELD(varlist)] [Table(string)] [CONT(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [GPLEVELPANEL(string)] [SPECIALCONTVARS(varlist)] [SPECIALCONT(varlist)] [REFERENCE(string)] [TREATMENT(varlist)]

  
	
	global suestestimates 
	global suesttest 	
	global ftest
	

	foreach cov in DVmean   Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  {

	foreach var in `panela' `panelb' `panelc' `paneld' {
		
		
		areg `var' Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3 i.Survey_Version i.survey_month  `cont' `if' `weight', clu(GroupNumber) a(Final_Randomization_Cluster) 

		
		if  "`cov'"!="N" & "`cov'"!="DVmean`j'" {
			local b= _b[`cov']
			local n1= r(p)
			local se = _se[`cov']
			
// 			local star ""
// 				if `n1'<=.01 local star "\sym{***}"
// 				if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
// 				if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
			local n2 = trim("`: display %10.3f `b''") 
			local `var'_combo "`n2'"
			
			local se = trim("`: display %10.3f `se''") 
			local `var'_se "(`se')"
			
					
			}

		if "`cov'"=="N" {
			local b= e(N)
			local n1=500
			local n2 = trim("`: display %10.0f `b''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
		sum `var' if Quint_Rank_NS_Tercile_1==1
		if "`cov'"=="DVmean`j'" {
			local mn=r(mean)
			local n2 = trim("`: display %10.3f `mn''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
	
	

}
foreach var in `panela' `panelb' `panelc' `paneld' {
	estadd local b_`var' ``var'_combo' 
	estadd local se_`var' ``var'_se'
}

estadd local PA ""
estadd local PB ""
estadd local PC ""
estadd local PD ""

est store `cov'
}



local cellsA "" 
foreach var in `panela' {
    local cellsA "`cellsA' b_`var' se_`var'"
}
local cellsB "" 
foreach var in `panelb' {
    local cellsB "`cellsB' b_`var' se_`var'"
}
local cellsC "" 
foreach var in `panelc' {
    local cellsC "`cellsC' b_`var' se_`var'"
}
local cellsD "" 
foreach var in `paneld' {
    local cellsD "`cellsD' b_`var' se_`var'"
}

	
	
local labstr "\textit{Panel A: Individual Characteristics of Ranked Entrepreneur}"
	foreach out in `panela' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
if "`panelb'"!="" {
	local labstr `"`labstr'" "\textit{Panel B: Sector of Ranked Entrepreneur}"'
	foreach out in `panelb' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`panelc'"!="" {
	local labstr `"`labstr'" "\textit{Panel C: Household Characteristics}"'
	foreach out in `panelc' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`paneld'"!="" {
	local labstr `"`labstr'" "\textit{Panel D: Characteristics of Household Businesses}"'
	foreach out in `paneld' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}


di "`labstr'"
di "`panelc'"
	
	esttab DVmean    Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  using "`table'", nostar noabbrev `type' replace nogaps /// 
		mlab("\specialcell{Bottom \\ Tercile\\ Rank \\ Mean}"  "\specialcell{Middle \\ Tercile\\ Rank \\Difference}" "\specialcell{Top \\ Tercile\\ Rank \\Difference}"   ) /// 
	stats(PA `cellsA' PB `cellsB' PC `cellsC' PD `cellsD', labels("`labstr'")) keep( ) ///
		nonote   noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'5`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		end
	
			
	
	